package com.koron.css2.meterService.impl;

import com.koron.css2.ImportExcelInterface;
import com.koron.css2.common.dto.HeaderInfoDto;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 换表信息导入
 * @author bin
 *
 */
public class MsChgImport implements ImportExcelInterface{

	@Override
	public MessageBean<?> importExel(SessionFactory factory, UserInfoBean userInfo, RequestBean req,List<Map<String, String>> data, MultipartFile file) {
		MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);

		if (data==null || data.size()==0){
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "请填写换表回填信息后导入", List.class);
		}
		for(Map<String, String> map : data) {
			//查询并转换换表员账号
			String replaceStaffName = map.get("replaceStaffName");
			if(StringUtils.isNotBlank(replaceStaffName)) {
				OrgUserMapper mapper = factory.getMapper(OrgUserMapper.class);
				OrgUserBean user = mapper.selectByName(replaceStaffName);
				if(user != null) {
					map.put("replaceStaff", user.getAccount());
				}
			}
			//检验新表编号是否存在
			if (map.containsKey("newMeterNo") && StringUtils.isNotEmpty(map.get("newMeterNo"))){
				MsInfoDto msInfo = msInfoMapper.getMsInfoByNo(map.get("newMeterNo"));
				if (msInfo==null)
					return MessageBean.create(Constant.MESSAGE_INT_FAIL, map.get("newMeterNo")+"新水表不存在", Void.class);
			}
			//检验必要字段是否为空
			if (StringUtils.isEmpty(map.get("newMeterNo")) || StringUtils.isEmpty(map.get("replaceDate"))||
					StringUtils.isEmpty(map.get("oldMeterNum"))||StringUtils.isEmpty(map.get("newMeterNum"))||
							StringUtils.isEmpty(map.get("oldHandleWay"))||StringUtils.isEmpty(map.get("replaceStaffName"))){
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "请完善导入模板内容再导入！", Void.class);
			}
		}
		@SuppressWarnings("rawtypes")
		MessageBean message = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "导入成功", List.class);
		message.setData(data);
		return message;
	}

	@Override
	public List<HeaderInfoDto> getHeaderInfo(UserInfoBean userInfo) {
		List<HeaderInfoDto> headerInfos = new ArrayList<HeaderInfoDto>();
		headerInfos.add(new HeaderInfoDto("用户编号", false, "userNo"));
		headerInfos.add(new HeaderInfoDto("旧水表编号", false, "oldMeterNo"));
		headerInfos.add(new HeaderInfoDto("换表日期", false, "replaceDate"));
		headerInfos.add(new HeaderInfoDto("旧表底码", false, "oldMeterNum"));
		headerInfos.add(new HeaderInfoDto("新表编号", false, "newMeterNo"));
		headerInfos.add(new HeaderInfoDto("新表起码", false, "newMeterNum"));
		headerInfos.add(new HeaderInfoDto("旧水表处理方式", false, "oldHandleWay"));
		headerInfos.add(new HeaderInfoDto("换表员", false, "replaceStaffName"));
		headerInfos.add(new HeaderInfoDto("备注", false, "comments"));
		return headerInfos;
	}

	@Override
	public boolean isCheckHeader() {
		return true;
	}

}
